﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ERPStudio;
using System.Data.SqlClient;

namespace ExBug
{
    public partial class OptiuniRapFrm : FrmCard
    {
        public String filtruArt = String.Empty;
        private Int16 tipRaport = 0;
        public OptiuniRapFrm(ModuleClass mc,Int16 tipRap)
            : base(mc)
        {
            InitializeComponent();
            deLaDTP.Format = DateTimePickerFormat.Custom;
            deLaDTP.CustomFormat = "dd.MM.yyyy";
            panaLaDTP.Format = DateTimePickerFormat.Custom;
            panaLaDTP.CustomFormat = "dd.MM.yyyy";
            tipRaport = tipRap;

        }

        private void OpriuniRapFoiBoalaFom_Load(object sender, EventArgs e)
        {
            SqlConnection con = DeschideConexiune(workConnString);
            this.balconSetupTableAdapter.Connection = con;
            this.balconSetupTableAdapter.Fill(this.eSRPDataSet.BalconSetup,ModuleInfo.CodUnitate);

            this.articoleAliniateTableAdapter.Connection = con;
            this.articoleAliniateTableAdapter.Fill(this.eSRPDataSet.ArticoleAliniate, ModuleInfo.CodUnitate);
            InchideConexiune(con);
            AddButton("Genereaza", global::ERPStudio.Properties.Resources._001_09, Genereaza, ToolStripItemAlignment.Right, false);

            if (balconSetupBindingSource.Count == 0)
                return;
            ESRPDataSet.BalconSetupRow bRowSetup = ((DataRowView)balconSetupBindingSource.Current).Row as ESRPDataSet.BalconSetupRow;
            DateTime inceputLuna = new DateTime(bRowSetup.AnCurent, bRowSetup.LunaCurenta, 1);
            DateTime sfarsitLuna = inceputLuna.AddMonths(1).AddDays(-1);
            deLaDTP.Value = inceputLuna;
            panaLaDTP.Value = sfarsitLuna;

        }

        private void Genereaza(object sender, EventArgs e)
        {
            String titlu = String.Empty;
            filtruArt = String.Empty;

            if (articoleGB.Visible && articolRB.Checked)
            {
                if (articolCB.SelectedValue!=null)
                    filtruArt = articolCB.SelectedValue.ToString();
            }
            if (articoleGB.Visible && articoleRB.Checked)
            {
                for (Int16 i = 0; i < ArticoleSelectateBindingSource.Count; i++)
                {
                    if (filtruArt != String.Empty)
                        filtruArt += ",";
                    filtruArt +=  ((FurnizoriArticoleSelectate)ArticoleSelectateBindingSource.List[i]).ToString() ;
                }
            }
            PreviewRaportFrm frm = new PreviewRaportFrm("RegistrulProiecte.rdlc", null, ModuleInfo);
            SqlConnection con = DeschideConexiune(workConnString);
            titlu += " în perioada " + deLaDTP.Value.Day.ToString() + "." + deLaDTP.Value.Month.ToString() + "." + deLaDTP.Value.Year.ToString() + " - " +
                panaLaDTP.Value.Day.ToString() + "." + panaLaDTP.Value.Month.ToString() + "." + panaLaDTP.Value.Year.ToString();

            if (tipRaport == 0)
            {    
                if (A4CB.Checked)
                    frm.NumeRaport = "RegistrulProiecteA4.rdlc";

                this.exbug_RegistrulProiectelorTableAdapter.Connection = con;
                this.exbug_RegistrulProiectelorTableAdapter.FillByRaport(this.eSRPDataSet.exbug_RegistrulProiectelor, ModuleInfo.CodUnitate,deLaDTP.Value,panaLaDTP.Value);
            }
            if (tipRaport == 1)
            {
                frm.NumeRaport = "RegistrulOrdonantari.rdlc";
                if (deLaTB.Text != String.Empty || panaLaTB.Text!=String.Empty)
                {
                    titlu += " între numerele " + deLaTB.Text + " - " + panaLaTB.Text;
                }
                Decimal delanr = -1, panalanr = -1;
                if (deLaTB.Text != String.Empty)
                {
                    Decimal.TryParse(deLaTB.Text, out delanr);
                }

                if (panaLaTB.Text != String.Empty)
                {
                    Decimal.TryParse(panaLaTB.Text, out panalanr);
                }
                this.exbug_GenRegistrulOrdonantariTableAdapter.Connection = con;
                this.exbug_GenRegistrulOrdonantariTableAdapter.Fill(this.eSRPDataSet.exbug_GenRegistrulOrdonantari, deLaDTP.Value,panaLaDTP.Value,filtruArt, delanr,panalanr, ModuleInfo.CodUnitate);

            }

            
            if (tipRaport == 0)
            {
                exbug_RegistrulProiectelorBindingSource.Sort = "NrDoc";
                frm.AddDataSource("exbug_RegistrulProiectelor", exbug_RegistrulProiectelorBindingSource);
            }
            if (tipRaport == 1)
            {
                exbug_GenRegistrulOrdonantariBindingSource.Sort = "NrDoc";
                frm.AddDataSource("exbug_RegistrulOrdonantari", exbug_GenRegistrulOrdonantariBindingSource);
            }
            frm.AddParameter("titlu", titlu, true);
            frm.MdiParent = ModuleInfo.mdiForm;
            frm.Show();
            InchideConexiune(con);

        }

        private void articoleAliniateBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.articoleAliniateBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.eSRPDataSet);

        }

        private void addArt_Click(object sender, EventArgs e)
        {
            ArticoleSelectateBindingSource.Add(new FurnizoriArticoleSelectate(articolCB.Text,0));
            articoleRB.Checked = true;
        }

        private void delArt_Click(object sender, EventArgs e)
        {
            ArticoleSelectateBindingSource.RemoveCurrent();
        }

        private void fillToolStripButton_Click(object sender, EventArgs e)
        {
            try
            {
                
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }

        }
    }

 
}
